<?php 
	defined('_JEXEC') or die('Restricted access');
	
	global $mainframe;

		// Initialize variables
		$db		=& JFactory::getDBO();
		$user	=& JFactory::getUser();

		//Ordering allowed ?
		$ordering = ($this->lists['order'] == 'section_name' && $this->lists['order_Dir'] == 'asc');
		JHTML::_('behavior.tooltip');
		?>
		<form action="index.php?option=com_calendar" method="post" name="adminForm">

			<table>
				<tr>
					<td width="100%">
						<?php echo JText::_( 'Filter' ); ?>:
						<input type="text" name="search" id="search" value="<?php echo $this->lists['search'];?>" class="text_area" onchange="document.adminForm.submit();" title="<?php echo JText::_( 'Filter by title or enter event ID' );?>"/>
						<button onclick="this.form.submit();"><?php echo JText::_( 'Go' ); ?></button>
						<button onclick="document.getElementById('search').value='';this.form.getElementById('catid').value='0';this.form.getElementById('filter_authorid').value='0';this.form.getElementById('filter_state').value='';this.form.submit();"><?php echo JText::_( 'Reset' ); ?></button>
					</td>
					<td nowrap="nowrap">
						<?php
						//echo $this->lists['sectionid'];
						echo $this->lists['catid'];
						//echo $this->lists['authorid'];
						echo $this->lists['state'];
						?>
					</td>
				</tr>
			</table>

			<table class="adminlist" cellspacing="1">
			<thead>
				<thead>
				<tr>
					<th width="10">
						<?php echo JText::_( 'Num' ); ?>
					</th>
					<th width="10" class="title">
						<input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($this->rows); ?>);" />
					</th>
					<th class="title">
						<?php echo JHTML::_('grid.sort',   'Event Title', 'a.title', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
					</th>
					<th width="5%" class="title" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Published', 'a.state', @$this->lists['order_Dir'], @$this->lists['order'] );//echo JHTML::_('grid.sort',   'Published', 'a.published', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
					</th>
                    <?php /*
					<th nowrap="nowrap" width="8%">
						<?php echo JHTML::_('grid.sort',   'Order by', 'cd.ordering', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
						<?php echo JHTML::_('grid.order',  $this->rows ); ?>
					</th>
					<th width="8%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'Access', 'cd.access', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
					</th>
					*/
					?>
					<th width="10%" class="title">
						<?php echo JHTML::_('grid.sort',   'Category', 'cat_name', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
					</th>
					<th class="title" nowrap="nowrap" width="10%">
						<?php echo JHTML::_('grid.sort',   'Event Time', 'a.start_date', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
					</th>
					<th width="1%" nowrap="nowrap">
						<?php echo JHTML::_('grid.sort',   'ID', 'a.id', @$this->lists['order_Dir'], @$this->lists['order'] ); ?>
					</th>
				</tr>
			</thead>
			</thead>
			<tfoot>
			<tr>
				<td colspan="7">
					<?php echo $this->pagination->getListFooter(); ?>
				</td>
			</tr>
			</tfoot>
            <tbody>
			<?php
			$k = 0;
			$nullDate = $db->getNullDate();
			//var_dump($this->rows);
			for ($i=0, $n=count( $this->rows ); $i < $n; $i++)
			{
				$row = &$this->rows[$i];

				$link 	= 'index.php?option=com_calendar&sectionid='. $redirect .'&task=edit&cid[]='. $row->id;

				//$row->sect_link = JRoute::_( 'index.php?option=com_sections&task=edit&cid[]='. $row->sectionid );
				$row->cat_link 	= JRoute::_( 'index.php?option=com_categories&section=com_calendar&task=edit&cid[]='. $row->catid );
				
				jimport('joomla.utilities.date');
				$config =& JFactory::getConfig();
				$now = new JDate();
				$publish_up = new JDate($row->publish_up);
				$publish_down = new JDate($row->publish_down);
				// uncomment to properly get offset from Joomla
				//$config_offset = $config->getValue('config.offset'); 
				
				//uncomment to override Joomla global offset
				//TODO: fix extensions to work properly with Joomla/PHP offset
				$config_offset = 0;
				$now->setOffset(-5);
				$local = true;
				
				//var_dump($now);
				$publish_up->setOffset($config_offset);
				$publish_down->setOffset($config_offset);
				if ( $now->toUnix($local) <= $publish_up->toUnix() && $row->state == 1 ) {
					$img = 'publish_y.png';
					$alt = JText::_( 'Published' );
				} else if ( ( $now->toUnix($local) <= $publish_down->toUnix() || $row->publish_down == $nullDate ) && $row->state == 1 ) {
					$img = 'publish_g.png';
					$alt = JText::_( 'Published' );
				} else if ( $now->toUnix($local) > $publish_down->toUnix() && $row->state == 1 ) {
					//var_dump($now->toMySql(true));
					//var_dump($publish_down->toMySql());
					$img = 'publish_r.png';
					$alt = JText::_( 'Expired' );
				} else if ( $row->state == 0 ) {
					$img = 'publish_x.png';
					$alt = JText::_( 'Unpublished' );
				} else if ( $row->state == -1 ) {
					$img = 'disabled.png';
					$alt = JText::_( 'Archived' );
				}
				$times = '';
				if (isset($row->publish_up)) {
					if ($row->publish_up == $nullDate) {
						$times .= JText::_( 'Start: Always' );
					} else {
						$times .= JText::_( 'Start' ) .": ". $publish_up->toFormat();
					}
				}
				if (isset($row->publish_down)) {
					if ($row->publish_down == $nullDate) {
						$times .= "<br />". JText::_( 'Finish: No Expiry' );
					} else {
						$times .= "<br />". JText::_( 'Finish' ) .": ". $publish_down->toFormat();
					}
				}

				if ( $user->authorize( 'com_users', 'manage' ) ) {
					if ( $row->created_by_alias ) {
						$author = $row->created_by_alias;
					} else {
						$linkA 	= 'index.php?option=com_users&task=edit&cid[]='. $row->created_by;
						$author = '<a href="'. JRoute::_( $linkA ) .'" title="'. JText::_( 'Edit User' ) .'">'. $row->author .'</a>';
					}
				} else {
					if ( $row->created_by_alias ) {
						$author = $row->created_by_alias;
					} else {
						$author = $row->author;
					}
				}
				
				$params = new JRegistry('com_calendar');
				//jimport('joomla.filesystem.file');
				//$data = JFile::read( JPATH_SITE . DS . 'language' . DS . 'en-GB' . DS . 'en-GB.ini' );
				//$registry->loadINI( $data );
				$params->loadINI($row->params);
				
				$access 	= JHTML::_('grid.access', $row, $i, $row->state );
				 //grid.checkedOut
				 
				//function mycheckedOut( &$row, $i, $identifier = 'id' )
			  //  {
					$user   =& JFactory::getUser();
					$userid = $user->get('id');
				//	$result = false;
				//	if(is_a($row, 'JTable')) {
				//		$result = $row->isCheckedOut($userid, $row->id);
				//	} else {
				//		$result = JTable::isCheckedOut($userid, $row->checked_out);
				//	}
				//	$checked = '';
				//	if ( $result ) {
				//		$checked = JHTMLGrid::_checkedOut( $row );
				//	} else {
				//		$checked = JHTML::_('grid.id', $i, $row->$identifier );
				//	}
				//	return JHTML::_('grid.id', $i, $row->$identifier );
					//return $checked;
				//}
				
				$checked 	= JHTML::_('grid.id', $i, $row->id );
				//$checked = JHTML::_('grid.checkedout',   $row, $i );
				//var_dump($row);
				?>
				<tr class="<?php echo "row$k"; ?>">
					<td>
						<?php echo $this->pagination->getRowOffset( $i ); ?>
					</td>
					<td align="center">
						<?php echo $checked; ?>
					</td>
					<td>
					<?php
						//checking in/out not coded completely; commenting logic flow
						
						//if (  JTable::isCheckedOut($user->get ('id'), $row->checked_out ) ) {
						//	echo $row->title;
						//} else if ($row->state == -1) {
						//	echo htmlspecialchars($row->title, ENT_QUOTES);
						//	echo ' [ '. JText::_( 'Archived' ) .' ]';
						//} else {
							?>
							<a href="<?php echo JRoute::_( $link ); ?>">
								<?php //echo htmlspecialchars($row->title, ENT_QUOTES);
								echo $row->title;
								 ?></a>
							<?php
						//}
						?>
					</td>
					<?php
					if ( $times ) {
						?>
						<td align="center">
							<span class="editlinktip hasTip" title="<?php echo JText::_( 'Publish Information' );?>::<?php echo $times; ?>"><a href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i;?>','<?php echo $row->state ? 'unpublish' : 'publish' ?>')">
								<img src="images/<?php echo $img;?>" width="16" height="16" border="0" alt="<?php echo $alt; ?>" /></a></span>
						</td>
						<?php
					}
					?><?php 
					/**
					* commented out frontpage
					*/
					/*
					<td align="center">
						<a href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i;?>','toggle_frontpage')" title="<?php echo ( $row->frontpage ) ? JText::_( 'Yes' ) : JText::_( 'No' );?>">
							<img src="images/<?php echo ( $row->frontpage ) ? 'tick.png' : ( $row->state != -1 ? 'publish_x.png' : 'disabled.png' );?>" width="16" height="16" border="0" alt="<?php echo ( $row->frontpage ) ? JText::_( 'Yes' ) : JText::_( 'No' );?>" /></a>
					</td>
					
					<td class="order" colspan="2">
						<span><?php echo $this->pagination->orderUpIcon( $i, ($row->catid == @$this->rows[$i-1]->catid), 'orderup', 'Move Up', $ordering); ?></span>
						<span><?php echo $this->pagination->orderDownIcon( $i, $n, ($row->catid == @$this->rows[$i+1]->catid), 'orderdown', 'Move Down', $ordering ); ?></span>
						
						<?php $disabled = $ordering ?  '' : 'disabled="disabled"'; ?>
						<input type="text" name="order[]" size="5" value="<?php echo $row->ordering; ?>" <?php echo $disabled; ?> class="text_area" style="text-align: center" />
					</td>
					*/
					?>
                    <?php 
					/**
					* commented out access
					*/
					/*
					<td align="center">
						<?php echo $access;?>
					</td>
					*/
					?>
					<?php 
					/**
					* commented out section //its com_calendar
					*/
					/*
						<td>
							<a href="<?php echo $row->sect_link; ?>" title="<?php echo JText::_( 'Edit Section' ); ?>">
								<?php echo $row->section_name; ?></a>
						</td>
					*/?>
					<td>
						<a href="<?php echo $row->cat_link; ?>" title="<?php echo JText::_( 'Edit Category' ); ?>">
							<?php echo $row->cat_name; ?></a>
					</td>
                    <?php 
					/**
					* commented out author/title; features to come?
					*/
					/*
					<td>
						<?php echo $row->author; ?>
					</td>
					<td>
						<?php echo $row->title; ?>
					</td>
					*/
					?>
					<td nowrap="nowrap">
						<?php  //echo $row->start_date." ".$row->start_time; 
						//	$event_date = $params->getValue( 'com_document.start_date');
							//$event_time = $params->getValue( 'com_document.start_time');
							echo JHTML::_('date',  $row->start_date." ".$row->start_time ,'%m-%d-%Y&nbsp;&nbsp;%I:%M %p' ); 
						?>
					</td>
                    <td>
						<?php echo $row->id; ?>
					</td>
                    <?php
                    /**
					* commented out hits; features to come?
					*/
					/*
					<td nowrap="nowrap" align="center">
						<?php echo $row->hits ?>
					</td>
					*/
					?>
				</tr>
				<?php
				$k = 1 - $k;
			}
			?>
			</tbody>
            <?php /*
             <tfoot>
                <tr>
					<td colspan="11">
						<?php echo $this->pagination->getListFooter(); ?>
					</td>
				</tr>
              </tfoot>
			 */ 
			 ?>
			</table>
			<?php //JHTML::_('calendar.legend'); ?>

		<input type="hidden" name="option" value="com_calendar" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="boxchecked" value="0" />
		<input type="hidden" name="redirect" value="<?php echo $redirect;?>" />
		<input type="hidden" name="filter_order" value="<?php echo $this->lists['order']; ?>" />
		<input type="hidden" name="filter_order_Dir" value="" />
		
		</form>